package com.sanyang.pda.ui.takestock;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.contract.ConfirmStockContract;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.presenter.ConfirmStockPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.Utils;
import com.sanyang.pda.view.ScanButView;
import com.sanyang.pda.view.datepicker.CustomDatePicker;
import com.sanyang.pda.view.datepicker.DateFormatUtils;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.ADDSTOCKACTIVITY)
public class AddStockActivity extends BaseMvpActivity<ConfirmStockPresenter> implements ConfirmStockContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.tv_kucun_amount)
    TextView tvKucunAmount;
    @BindView(R.id.tv_shiji)
    EditText tvShiji;
    @BindView(R.id.tv_kuwei)
    TextView tvKuwei;
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.tv_goods_code)
    TextView tvGoodsCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_guige)
    TextView tvGuige;
    @BindView(R.id.tv_pici)
    EditText tvPici;
    @BindView(R.id.tv_time_start)
    TextView tvTimeStart;
    @BindView(R.id.tv_product_time)
    TextView tvProductTime;
    @BindView(R.id.tv_admire_date)
    TextView tvAdmireDate;
    @BindView(R.id.bt_confirm)
    Button btConfirm;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.bt_scan)
    ScanButView btScan;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    private String storagebinId, taskNo;
    ByBarcodeBean byBarcodeBean;

    @Override
    public ConfirmStockPresenter getPresenter() {
        return new ConfirmStockPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_addstock;
    }

    @Override
    public void initView() {
        tvTitle.setText(getString(R.string.add));
        tvKuwei.setText(storagebinId);
        initDatePicker();
    }

    @Override
    public void initLinsenterner() {
        btScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case -1://商品
                        //tvGoodsName.setText(scanBean.getCode());
                        presenter.selectArticleInfoByBarcode(scanBean.getCode(),null);
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        break;
                }
            }
        });
    }

    @Override
    public void getData() {
        storagebinId = getIntent().getExtras().getString(StringConfig.STORAGEBINID, "");
        taskNo = getIntent().getExtras().getString(StringConfig.TASKNO, "");
    }


    @OnClick(R.id.iv_back)
    public void onIvBackClicked() {
        finishActivity();
    }

    @OnClick(R.id.tv_cancel)
    public void onTvCancelClicked() {
        finishActivity();
    }

    @OnClick(R.id.bt_confirm)
    public void onBtConfirmClicked() {
        if (TextUtils.isEmpty(tvTray.getText())) {
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvKuwei.getText())) {
            showMessage(getString(R.string.storage_location) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvName.getText())) {
            showMessage(getString(R.string.commodity) + getString(R.string.not_be_empty));
            return;
        }
       /* if (TextUtils.isEmpty(tvPici.getText())) {
            showMessage(getString(R.string.batch) + getString(R.string.not_be_empty));
            return;
        }*/
        if (TextUtils.isEmpty(tvShiji.getText())) {
            showMessage(getString(R.string.amount) + getString(R.string.not_be_empty));
            return;
        }
       /* if (TextUtils.isEmpty(tvProductTime.getText())) {
            showMessage(getString(R.string.date_in_produced) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvAdmireDate.getText())) {
            showMessage(getString(R.string.admire_date) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvTimeStart.getText())) {
            showMessage(getString(R.string.delivery_time) + getString(R.string.not_be_empty));
            return;
        }*/
        TakeStockBean takeStockBean=new TakeStockBean();
        takeStockBean.setActualNum(Double.parseDouble(tvShiji.getText().toString()));
        takeStockBean.setTaskNo(taskNo);
        takeStockBean.setArticleId(byBarcodeBean.getArticleId());
        takeStockBean.setStoragebinId(storagebinId);
        takeStockBean.setSpec(byBarcodeBean.getSpec());
        takeStockBean.setArticleName(byBarcodeBean.getArticleName());
        takeStockBean.setArticleUnit(byBarcodeBean.getArticleUnit());
        takeStockBean.setBatchNo(tvPici.getText().toString());
        takeStockBean.setEntryTime(tvTimeStart.getText().toString());
        takeStockBean.setProductTime(tvProductTime.getText().toString());
        takeStockBean.setExpiringTime(tvAdmireDate.getText().toString());
        takeStockBean.setPallet(tvTray.getText().toString());
        presenter.stockAdd(takeStockBean);
    }

    List<SelectBean> selectBeanList=new ArrayList<>();
    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0){
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1){
            this.byBarcodeBean = list.get(0);
            tvGoodsCode.setText(byBarcodeBean.getBarcode());
            tvGuige.setText(byBarcodeBean.getSpec());
            tvName.setText(byBarcodeBean.getArticleName());
            tvUnit.setText(Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
            tvKucunAmount.setText(getString(R.string.inventory_number) + "     " + 0 + Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
        }
        if (list.size() > 1){
            selectBeanList.clear();
            for (int i=0;i< list.size();i++){
                ByBarcodeBean byBarcodeBean=list.get(i);
                SelectBean selectBean=new SelectBean(byBarcodeBean.getArticleName(),byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getSpec());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop=new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                    for (ByBarcodeBean itme: list){
                        if (itme.getArticleId().equals(content.getId())){
                            byBarcodeBean=itme;
                        }
                        tvGoodsCode.setText(byBarcodeBean.getBarcode());
                        tvGuige.setText(byBarcodeBean.getSpec());
                        tvName.setText(byBarcodeBean.getArticleName());
                        tvUnit.setText(Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
                        tvKucunAmount.setText(getString(R.string.inventory_number) + "     " + 0 + Utils.getArticleUnit(mContext, byBarcodeBean.getArticleUnit()));
                    }
                }
            });
            selectPop.show(selectBeanList);
        }
    }

    CustomDatePicker mDatePicker;
    private void initDatePicker() {
        long beginTimestamp = DateFormatUtils.str2Long("2000-01-01", false);
        long endTimestamp = DateFormatUtils.str2Long("2500-01-01", false);
        // 通过时间戳初始化日期，毫秒级别
        mDatePicker = new CustomDatePicker(mContext, new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp, TextView v) {
                v.setText(DateFormatUtils.long2Str(timestamp, false));
            }
        }, beginTimestamp, endTimestamp);
    }
    @OnClick({R.id.tv_time_start, R.id.tv_product_time, R.id.tv_admire_date})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_time_start:
                mDatePicker.show(System.currentTimeMillis(),tvTimeStart);
                break;
            case R.id.tv_product_time:
                mDatePicker.show(System.currentTimeMillis(),tvProductTime);
                break;
            case R.id.tv_admire_date:
                mDatePicker.show(System.currentTimeMillis(),tvAdmireDate);
                break;
        }
    }
}
